﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KBCommon.Utility
{
    /// <summary>
    /// 数据缓存保存信息异步处理委托
    /// </summary>
    delegate void EventSaveCache(object key, object value);
    /// <summary>
    /// 对象缓存类
    /// </summary>
    public class Cache
    {
        //private MappingContainer mContainer;
        ///// <summary>
        ///// 获取或设置当前缓存对象所在的关系映象容器
        ///// </summary>
        //public MappingContainer Container
        //{
        //    get
        //    {
        //        return mContainer;
        //    }
        //    set
        //    {
        //        mContainer = value;
        //    }
        //}
        /// <summary>
        /// 构造缓存对象
        /// </summary>
        public Cache()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 用于缓存数据的Hashtable
        /// </summary>
        protected System.Collections.Hashtable _Cache = new System.Collections.Hashtable();
        protected Object _LockObj = new object();
        /// <summary>
        /// 获取指定键值的对象
        /// </summary>
        /// <param name="key">键值</param>
        /// <returns>object</returns>
        public virtual object GetObject(object key)
        {
            if (_Cache.ContainsKey(key))
                return _Cache[key];
            return null;
        }
        /// <summary>
        /// 把对象按指定的键值保存到缓存中
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="value">保存的对象</param>
        //public void SaveCache(object key, object value)
        //{
        //    EventSaveCache save = new EventSaveCache(SetCache);
        //    IAsyncResult ar = save.BeginInvoke(key, value, new System.AsyncCallback(Results), null);
        //}
        //private void Results(IAsyncResult ar)
        //{
        //    EventSaveCache fd = (EventSaveCache)((AsyncResult)ar).AsyncDelegate;
        //    fd.EndInvoke(ar);
        //}
        /// <summary>
        /// 把对象按指定的键值保存到缓存中
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="value">保存的对象</param>
        protected virtual void SetCache(object key, object value)
        {
            lock (_LockObj)
            {
                if (!_Cache.ContainsKey(key))
                    _Cache.Add(key, value);
            }
        }
        public int Count
        {
            get
            {
                return _Cache.Count;
            }
        }
        /// <summary>
        /// 在缓存中删除指定键值的对象
        /// </summary>
        /// <param name="key">键值</param>
        public virtual void DelObject(object key)
        {
            lock (_Cache.SyncRoot)
            {
                _Cache.Remove(key);
            }
        }
        /// <summary>
        /// 清除缓存中所有对象
        /// </summary>
        public virtual void Clear()
        {
            lock (_Cache.SyncRoot)
            {
                _Cache.Clear();
            }
        }
    }

}
